package com.live.dengdengtv.activity.msg;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.CustomTitleActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.gson.PersonInfoItem;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.EmptyViewUtil;

import java.util.ArrayList;

/**
 * Created by wyf on 2016/8/18.
 */
public class FriendListActivity extends CustomTitleActivity implements SwipeRefreshLayout.OnRefreshListener{


    private static final String TAG = FollowActivity.class.getName();
    private ListView listView;
//    private SwipeRefreshLayout swipeLayout;
    private BaseAdapter adapter;
    private ArrayList<PersonInfoItem> listViewData = new ArrayList<PersonInfoItem>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_follow,R.layout.title_custom_red);
        setDefaultTitle("关注好友");

        listView = (ListView) findViewById(R.id.listView);
        setListView();

//        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
//        swipeLayout.setColorSchemeResources(R.color.app_red);
//        swipeLayout.setOnRefreshListener(this);

        View emptyView = EmptyViewUtil.getEmptyView(getApplicationContext(),"这里什么都没有哦~");
        ((ViewGroup)listView.getParent()).addView(emptyView);
        listView.setEmptyView(emptyView);

        getFriendList();
    }
    private void getFriendList(){
        requestData(NetTaskList.friend_list, AppInfo.getHeadInfo(),1);
    }

    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if (id == 1) {
            listViewData.clear();
            if(response.result == 0){
                JsonArray array = response.data.getAsJsonArray();
                for (int i=0; i<array.size() ;i++){
                    PersonInfoItem liveItem = new Gson().fromJson(array.get(i).getAsJsonObject(), PersonInfoItem.class);
                    listViewData.add(liveItem);
                }
            }
            adapter.notifyDataSetChanged();
//                swipeLayout.setRefreshing(false);

        }
        return false;
    }

    @Override
    public void onRefresh() {
        getFriendList();
    }

    private View setListView() {
        adapter = new MyBaseAdapter();

        listView.setAdapter(adapter);

        return listView;
    }

    private class MyBaseAdapter extends  BaseAdapter{
        @Override
        public int getCount() {
            return listViewData.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView != null) {
                holder = (ViewHolder)convertView.getTag();
            } else {
                convertView = View.inflate(context, R.layout.card_people_info, null);

                holder = new ViewHolder();
                holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
                holder.msg_tv = (TextView) convertView.findViewById(R.id.msg_tv);
                holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
                holder.head_iv = (ImageView) convertView.findViewById(R.id.head_iv);
                holder.sex_iv = (ImageView) convertView.findViewById(R.id.sex_iv);
                holder.level_iv = (ImageView) convertView.findViewById(R.id.level_iv);

                convertView.setTag(holder);
            }

            holder.name_tv.setText(listViewData.get(position).getNickname());
            holder.msg_tv.setText(listViewData.get(position).getSelf_description());
            holder.time_tv.setVisibility(View.GONE);
            return convertView;
        }
    }

    private static class ViewHolder{
        TextView name_tv;
        TextView msg_tv;
        TextView time_tv;
        ImageView head_iv;
        ImageView sex_iv;
        ImageView level_iv;
    }
}
